package com.easy.car.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.car.mapper.CarMapper;
import com.easy.common.bean.Car;
import com.easy.common.service.ICarService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {
    @Override
    public Car getCarById(Integer id) {
        return super.getById(id);
    }

    @Override
    public int addCar(Car car) {
        return super.save(car) ? 1 : 0;
    }

    @Override
    public int updateCar(Car car) {
        return super.updateById(car) ? 1 : 0;
    }

    @Override
    public int deleteCar(Integer id) {
        Car car = super.getById(id);
        if (car != null) {
            car.setIsdel(1);
            return super.updateById(car) ? 1 : 0;
        }
        return 0;
    }

    @Override
    public List<Car> getAll(Car car) {
        LambdaQueryWrapper<Car> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(car.getCar_number() != null, Car::getCar_number, car.getCar_number());
        queryWrapper.like(car.getCar_model() != null, Car::getCar_model, car.getCar_model());
        queryWrapper.like(car.getCar_brand() != null, Car::getCar_brand, car.getCar_brand());
        queryWrapper.eq(car.getDriver_id() != null, Car::getDriver_id, car.getDriver_id());
        queryWrapper.eq(Car::getIsdel, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<Car> getPage(Car car, IPage page) {
        LambdaQueryWrapper<Car> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(car.getCar_number() != null, Car::getCar_number, car.getCar_number());
        queryWrapper.like(car.getCar_model() != null, Car::getCar_model, car.getCar_model());
        queryWrapper.like(car.getCar_brand() != null, Car::getCar_brand, car.getCar_brand());
        queryWrapper.eq(car.getDriver_id() != null, Car::getDriver_id, car.getDriver_id());
        queryWrapper.eq(Car::getIsdel, 0);
        return super.page(page, queryWrapper);
    }
}
